<?php

class AdminUserController extends Controller
{

    //后台用户登录
    public function actionDoLogin()
    {

        try {
            //参数验证
            $user_name = Util::getParamNotEmpty("user_name");
            $password = Util::getParamNotEmpty("password");

            //输入合法性验证
            AdminUserService::vaildAdminUser($user_name);
            AdminUserService::vaildAdminPassword($password);

            //登录操作
            $admin_user = AdminUserService::login($user_name, $password);

            //生成session
            $admin_user->session_id = SessionService::generateSession($admin_user->user_id,MsgConst::SESSION_TYPE_ADMIN);

            //返回数据

            Util::responseObj(AdminUserService::formatAdminUser($admin_user));

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
     * session check
     *
     */
    public function actionSessionCheck()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            Util::responseObj(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 退出登录
     *
     */
    public function actionLogout()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //返回数据

            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
     * getmenulist
     *
     */
    public function actiongetmenulist()
    {

        Util::log_normal($this,"menu start");
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //获取role id
            $roleid = AdminRoleService::getRoleIDByUserID($user_id);

            //menulist
            //$Html = AdminRoleService::getValidMenuList($roleid);


            //转化为html
           // $Html = AdminRoleService::stringToHtml($menulist);



            //初始化
            $searchsql = Util::initSearchSql();
            $returnlist = AdminMenuService::getMenuList($searchsql);

            Util::log_normal($this,"get menu list");

            $alllist = array();

            for($i=0;$i<count($returnlist);$i++)
            {
                $is_select = AdminRoleMenuService::checkMenuAndRole($returnlist[$i]->menu_id,$roleid);

                if($is_select ==1)
                    array_push($alllist,$returnlist[$i]);
            }

            Util::log_normal($this,"check every menu already");


            //返回数据

            //Util::responseList(count($menulist),$menulist);

            Util::responseObj(AdminRoleService::TransfetToHtml($alllist));

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
     * 获取用户列表
     */
    public function actionGetAdminUserList()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //初始化
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"user_name","user_name"); //用户名

            //获取count
            $totalcount = AdminUser::model()->count($searchsql);



            //排序
            $searchsql = $searchsql." order by user_id asc";
            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //Util::responseMsg($searchsql);

            //获取list
            $userlist = AdminUser::model()->findAll($searchsql);

            //设置角色名
            for($i=0;$i<count($userlist);$i++)
            {
                $role = AdminRole::model()->findByPK($userlist[$i]->role_id);

                if(empty($role)==false)
                    $userlist[$i]->role_name = $role->role_name;
            }



            // Util::responseObj(MsgConst::RETURN_SUCCESS);
            Util::responseList($totalcount,$userlist);




        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    //添加后台用户
    public function actionAddAdminUser()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $user_name = Util::getParamNotEmpty("user_name");
            $role_id = Util::getParamNotEmpty("role_id");
            $real_name = Util::getParamNotEmpty("real_name");

            AdminUserService::vaildAdminUser($user_name);

            //新增
            AdminUserService::addAdminUser($user_name,$role_id,$real_name);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    //删除用户
    public function actionDeleteAdminUser()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $user_id_DELETE = Util::getParamNotEmpty("user_id");

            if($user_id == $user_id_DELETE)
            {
                throw new Exception("不能删除自己");
            }







            //新增
            AdminUserService::deleteUser($user_id_DELETE);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    //重置密码

    public function actionResetPassword()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $user_id = Util::getParamNotEmpty("user_id");



            //重置
            AdminUserService::resetPassword($user_id);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    //修改密码

    public function actionChangePassword()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $password = Util::getParamNotEmpty("password");
            $oldpassword = Util::getParamNotEmpty("oldpassword");
            //修改
            AdminUserService::changePassword($user_id,$password,$oldpassword);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }

    }



    //修改后台用户信息

    public function actionChangeUserInfo()
    {
        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $user_id = Util::getParamNotEmpty("user_id");
            $role_id = Util::getParamNotEmpty("role_id");
            $real_name = Util::getParamNotEmpty("real_name");




            //重置
            AdminUserService::changeUserInfo($user_id,$role_id,$real_name);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }




}